<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-type" content="text/html;charset=utf-8">
</head>
<style type="text/css">
    body{
        font-size: 12px;font-family: verdana;width: 100%;
    }
    div.page{
        text-align: center;
    }
    div.content{
        height: 250px;
    }
    div.page a{
        border:#aaaadd 1px solid;text-decoration: none;padding: 2px 5px 2px 5px;margin: 2px;
    }
    div.page span.current{
        border:#000099 1px solid;background-color: #000099;padding: 2px 5px 2px 5px;margin: 2px;color: #fff;font-weight: bold;
    } 
    div.page span.disable{
        border:#eee 1px solid;padding:2px 5px 2px 5px; margin: 2px;color:#ddd;
    }
    div.page form{
        display: inline;
    }
</style>
<body>
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
?>
<?php
/** 1.传入页面 **/
$page= isset($_GET['p']) ? trim($_GET['p']) : 1;
header("Content-Type: text/html; charset=utf-8");//防止界面乱码
/** 2.根据页面取出数据：php->mysql **/
$host = "localhost";
$username = 'root';
$password = '';
$db = 'tumor';
$PageSize=10;
$ShowPage=3;
//连接数据库
$conn = mysql_connect($host, $username, $password);
if(!$conn){
 // echo "数据库连接失败";
 exit;
}

//选择所要操作的数据库
mysql_select_db($db);
//设置数据库编码格式
mysql_query('SET NAMES UTF8');
//编写sql获取分页数据：SELECT * FROM 表名 LIMIT 起始位置 , 显示条数
$sql = "SELECT * FROM cna_genes LIMIT ".($page-1)*$PageSize .",$PageSize";
if(!$sql){
 echo "取出不成功";
};
//把sql语句传送到数据库
$result = mysql_query($sql);
//处理我们的数据
echo "<div class='content'>";
echo "<table border=1 cellspacing=0 width=50% align='center'>";
echo "<caption>cna_genes FROM cBioPortal for Cancer Genomics</caption>";
echo "<tr>
<th>ID</th>
<th>Gene</th>
<th>Gistic(Q-value)</th>
<th>Cytoband</th>
<th>CNA</th>
<th>Profiled Samples</th>
<th>#</th>
<th>Freq</th>
<th>Is Cancer Gene (source: OncoKB)</th>
</tr>";
while($row = mysql_fetch_assoc($result)){
 echo "<tr>";
 echo "<td>{$row['ID']}</td>";
 echo "<td>{$row['Gene']}</td>";
 echo "<td>{$row['Gistic(Q-value)']}</td>";
 echo "<td>{$row['Cytoband']}</td>";
 echo "<td>{$row['CNA']}</td>";
 echo "<td>{$row['Profiled Samples']}</td>";
 echo "<td>{$row['#']}</td>";
 echo "<td>{$row['Freq']}</td>";
 echo "<td>{$row['Is Cancer Gene (source: OncoKB)']}</td>";
 echo "<tr>";
}
echo "</table>";
echo "</div>";
//释放结果
mysql_free_result($result);

//获取数据总数
$to_sql="SELECT COUNT(*)FROM cna_genes";
$to_result=mysql_fetch_array(mysql_query($to_sql));
$to=$to_result[0];
//计算页数
$to_pages=ceil($to/$PageSize);
mysql_close($conn);
/** 3.显示数据+分页条 **/
$page_banner="<div class='page'>";
//计算偏移量
$pageffset=($ShowPage-1)/2;
if($page>1){
 $page_banner.="<a href='".$_SERVER['PHP_SELF']."?p=1'>首页</a>";
 $page_banner.="<a href='".$_SERVER['PHP_SELF']."?p=".($page-1)."'><上一页</a>";
}
//初始化数据
$start=1;
$end=$to_pages;
if ($to_pages>$ShowPage){
 if($page>$pageffset+1){
 $page_banner.="...";
 }
 if ($page>$pageffset){
 $start=$page-$pageffset;
 $end=$to_pages>$page+$pageffset?$page+$pageffset:$to_pages;
 }else{
 $start=1;
 $end=$to_pages>$ShowPage?$ShowPage:$to_pages;
 }
 if ($page+$pageffset>$to_pages){
 $start=$start-($page+$pageffset-$end);
 }
}
for($i=$start;$i<=$end;$i++) {
 if ($page == $i) {
 $page_banner .= "<span class='current'>{$i}</span>";
 } else {
 $page_banner .= "<a href='" . $_SERVER['PHP_SELF'] . "?p=" . ($i) . "'>{$i}</a>";
 }
}
//尾部省略
if ($to_pages>$ShowPage&&$to_pages>$page+$pageffset){
 $page_banner.="...";
}
if ($page<$to_pages){
 $page_banner.="<a href='".$_SERVER['PHP_SELF']."?p=".($page+1)."'>下一页></a>";
 $page_banner.="<a href='".$_SERVER['PHP_SELF']."?p=".($to_pages)."'>尾页</a>";
}

$page_banner.="共{$to_pages}页；合计{$to}条记录";
$page_banner.="<form action='mupage.php' method='get'>";
$page_banner.="  跳到第<input type='text'size='2'name='p'>页";
$page_banner.="<input type='submit'value='确定'>";
$page_banner.="</form></div>";
echo $page_banner;

?>
</body>
</html>
